#include "sys_config.h"
#include "typesdef.h"
#include "lib/video/dvp/cmos_sensor/csi.h"
#include "tx_platform.h"
#include "list.h"
#include "dev.h"
#include "hal/i2c.h"


#if DEV_SENSOR_GC0308


SENSOR_INIT_SECTION static const unsigned char GC0308InitTable[CMOS_INIT_LEN]=
{
	0xFE,0x80,
	0xFE,0x00,
	0xD2,0x10,
	0x22,0x55,
	0x5A,0x56,
	0x5B,0x40,
	0x5C,0x4A,
	0x22,0x57,
	0x01,0xCE,
	0x02,0x70,
	0x0F,0x00,
	0xE2,0x00,
	0xE3,0x96,
	0xE4,0x02,
	0xE5,0x58,
	0xE6,0x02,
	0xE7,0x58,
	0xE8,0x02,
	0xE9,0x58,
	0xEA,0x0C,
	0xEB,0xBE,
	0xEC,0x20,
	0x05,0x00,
	0x06,0x00,
	0x07,0x00,
	0x08,0x00,
	0x09,0x01,
	0x0A,0xE8,
	0x0B,0x02,
	0x0C,0x88,
	0x0D,0x02,
	0x0E,0x02,
	0x10,0x22,
	0x11,0xFD,
	0x12,0x2A,
	0x13,0x00,
	0x14,0x10,
	0x15,0x0A,
	0x16,0x05,
	0x17,0x01,
	0x18,0x44,
	0x19,0x44,
	0x1A,0x1E,
	0x1B,0x00,
	0x1C,0xC1,
	0x1D,0x08,
	0x1E,0x60,
	0x1F,0x16,
	0x20,0xFF,
	0x21,0xF8,
	0x22,0x57,
	0x24,0xA2,
	0x25,0x0F,
	0x26,0x03,
	0x2F,0x01,
	0x30,0xF7,
	0x31,0x50,
	0x32,0x00,
	0x39,0x04,
	0x3A,0x18,
	0x3B,0x20,
	0x3C,0x00,
	0x3D,0x00,
	0x3E,0x00,
	0x3F,0x00,
	0x50,0x10,
	0x53,0x82,
	0x54,0x80,
	0x55,0x80,
	0x56,0x82,
	0x57,0x80,
	0x58,0x80,
	0x59,0x80,
	0x8B,0x40,
	0x8C,0x40,
	0x8D,0x40,
	0x8E,0x2E,
	0x8F,0x2E,
	0x90,0x2E,
	0x91,0x3C,
	0x92,0x50,
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FA,0x78,
	0xFE,0x01,
	0x00,0xF5,
	0x02,0x1A,
	0x0A,0xA0,
	0x0B,0x60,
	0x0C,0x08,
	0x0E,0x4C,
	0x0F,0x39,
	0x11,0x3F,
	0x12,0x72,
	0x13,0x13,
	0x14,0x42,
	0x15,0x43,
	0x16,0xC2,
	0x17,0xA8,
	0x18,0x18,
	0x19,0x40,
	0x1A,0xD0,
	0x1B,0xF5,
	0x70,0x40,
	0x71,0x58,
	0x72,0x30,
	0x73,0x48,
	0x74,0x20,
	0x75,0x60,
	0x77,0x20,
	0x78,0x32,
	0x30,0x03,
	0x31,0x40,
	0x32,0xE0,
	0x33,0xE0,
	0x34,0xE0,
	0x35,0xB0,
	0x36,0xC0,
	0x37,0xC0,
	0x38,0x04,
	0x39,0x09,
	0x3A,0x12,
	0x3B,0x1C,
	0x3C,0x28,
	0x3D,0x31,
	0x3E,0x44,
	0x3F,0x57,
	0x40,0x6C,
	0x41,0x81,
	0x42,0x94,
	0x43,0xA7,
	0x44,0xB8,
	0x45,0xD6,
	0x46,0xEE,
	0x47,0x0D,
	0xFE,0x00,
	0xD2,0x90,
	0xFE,0x00,
	0x10,0x26,
	0x11,0x0D,
	0x1A,0x2A,
	0x1C,0x49,
	0x1D,0x9A,
	0x1E,0x61,
	0x3A,0x20,
	0x50,0x14,
	0x53,0x80,
	0x56,0x80,
	0x8B,0x20,
	0x8C,0x20,
	0x8D,0x20,
	0x8E,0x14,
	0x8F,0x10,
	0x90,0x14,
	0x94,0x02,
	0x95,0x07,
	0x96,0xE0,
	0xB1,0x40,
	0xB2,0x40,
	0xB3,0x40,
	0xB6,0xE0,
	0xD0,0xCB,
	0xD3,0x48,
	0xF2,0x02,
	0xF7,0x12,
	0xF8,0x0A,
	0xFE,0x01,
	0x02,0x20,
	0x04,0x10,
	0x05,0x08,
	0x06,0x20,
	0x08,0x0A,
	0x0E,0x44,
	0x0F,0x32,
	0x10,0x41,
	0x11,0x37,
	0x12,0x22,
	0x13,0x19,
	0x14,0x44,
	0x15,0x44,
	0x19,0x50,
	0x1A,0xD8,
	0x32,0x10,
	0x35,0x00,
	0x36,0x80,
	0x37,0x00,
	0xFE,0x00,
	0x14,0x10,

		-1,-1,
};

extern struct i2c_device *iic_test;

static void GC0308_rotate(uint32 r)
{
	int8 buf[2];
	buf[0] = 0x14;		//0',180'
	buf[1] = 0x10|(r<<0);
	//Sensor_WriteRegister(SENSOR_IIC,buf,1,1);
	//i2c_sensor_write(iic_test,buf,1,1,0x42,0x43);
	i2c_write(iic_test,buf,1,&buf[1],1);
	

}



uint32 GC0308_hvblank(int8 dh,int8 dv)
{
#define HBLANK_BASE	0x6aUL
#define VBLANK_BASE	0x12UL
	uint32 i;
	int32 h = (int32)HBLANK_BASE + dh; if(h < 0) h = 0;
	int32 v = (int32)VBLANK_BASE + dv; if(v < 0) v = 0;
	const uint8 t[][2] = {
		{0x01,h & 0xff},//0x6a//0xde //HB[7:0]
		{0x02,v & 0xff},  //0x0c //VB[7:0]
		{0x0f,((v >> 8)<<4)|(h >> 8)} //{0x0f,0x00}, //VB/HB [11:8]
	};
	
	//for(i=0;i<3;i++)
		//Sensor_WriteRegister(SENSOR_IIC,(uint8 *)&t[i][0],1,1);
	for(i=0;i<3;i++)	
		//i2c_sensor_write(iic_test,(uint8 *)&t[i][0],1,1,0x42,0x43);
		i2c_write(iic_test,(int8 *)&t[i][0],1,(int8 *)&t[i][1],1);

	return (h<<16)|v;
}

SENSOR_OP_SECTION const _Sensor_Adpt_ gc0308_cmd= 
{
	.typ = 1, //YUV
	.pixelw = 640,
	.pixelh= 480,
	.hsyn = 1,
	.vsyn = 1,
	.rduline = 0,//
	.rawwide = 1,//10bit
	.colrarray = 2,//0:_RGRG_ 1:_GRGR_,2:_BGBG_,3:_GBGB_
	.init = (uint8 *)GC0308InitTable,
	.rotate_adapt = {0},
	//.hvb_adapt = {0x6a,0x12,0x6a,0x12},
	. mclk = 24000000,
	.p_fun_adapt = {GC0308_rotate,GC0308_hvblank,NULL},
	.p_xc7016_adapt = {NULL},	
};

const _Sensor_Ident_ gc0308_init=
{
	0x9b,0x42,0x43,0x01,0x01,0x00
};



#endif

